<template>
  <div class="wrap">
    <div class="search-row">
      <el-input class="keyword" placeholder="请输入搜索内容" v-model="searchWord" clearable>
        <el-button slot="append" icon="el-icon-search" @click="refreshList"></el-button>
      </el-input>
      <el-button class="download" type="primary" @click="download">导出校验清单</el-button>
      <div class="row">
        <div class="date-desc">首贷户月份:</div>
        <el-date-picker
          format="yyyy年MM月"
          value-format="yyyy年MM月"
          v-model="month"
          type="month"
          placeholder="选择月"
          @change="refreshList"
        ></el-date-picker>
        <div class="org-desc">首贷银行:</div>
        <el-select v-model="orgList.selected" @change="refreshList" clearable placeholder="请选择">
          <el-option
            v-for="item of orgList.list"
            :key="item.orgId"
            :label="item.orgName"
            :value="item.orgId"
          ></el-option>
        </el-select>
        <el-button
          v-if="month != null"
          class="download"
          type="danger"
          @click="deleteBatch"
        >清空{{month}}数据</el-button>
      </div>
    </div>
    <el-table :data="firstLoanResultTable" stripe style="width: 100%;">
      <el-table-column prop="reportDate" label="日期" width="100"></el-table-column>
      <el-table-column prop="reportOrg" label="填报银行名称" width="120"></el-table-column>
      <el-table-column prop="firmName" label="企业名称" width="200"></el-table-column>
      <el-table-column prop="reason" label="错误原因"></el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalPage"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="pageChange"
    ></el-pagination>
  </div>
</template>
<script>
import {
  Table,
  TableColumn,
  Pagination,
  Input,
  Button,
  DatePicker,
  Select,
  Option
} from "element-ui";
import {
  getFirstLoanResultList,
  getAllFirstLoanOrgList,
  deleteFirstLoanResultByMonth,
  downloadResult
} from "@/api/first-loan.js";

import {downloadFile} from "@/utils/download.js";
export default {
  name: "first-loan-result",
  components: {
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Pagination.name]: Pagination,
    [Input.name]: Input,
    [Button.name]: Button,
    [DatePicker.name]: DatePicker,
    [Select.name]: Select,
    [Option.name]: Option
  },
  data() {
    return {
      firstLoanResultTable: [],
      pageNum: 1,
      pageSize: 10,
      totalPage: 0,
      searchWord: null,
      month: null,
      orgList: {
        selected: null,
        list: []
      }
    };
  },
  methods: {
    refreshList() {
      getFirstLoanResultList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        month: this.month,
        orgId: this.orgList.selected,
        searchWord: this.searchWord
      }).then(res => {
        this.firstLoanResultTable = res.list;
        this.totalPage = res.total;
      });
    },
    pageChange(page) {
      this.pageNum = page;
      this.refreshList();
    },
    download() {
      let loadingInstance = this.$loading({
        fullscreen: true,
        text: "正在下载首贷户校验结果"
      })
      downloadResult({
        month: this.month,
        orgId: this.orgList.selected,
        searchWord: this.searchWord
      })
        .then(res => {
          downloadFile(res, window, this.$alert);
          loadingInstance.close();
        })
        .catch(errMsg => this.$alert(errMsg));
    },
    deleteBatch() {
      if (this.month == null) {
        return;
      }
      deleteFirstLoanResultByMonth(this.month)
        .then(res => {
          this.refreshList();
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    }
  },
  watch: {
    searchWord() {
      this.refreshList();
    }
  },
  mounted() {
    this.refreshList();
    getAllFirstLoanOrgList()
      .then(res => {
        this.orgList.selected = -1;
        this.orgList.list.push({ orgId: -1, orgName: "全部" });
        res.forEach(item => this.orgList.list.push(item));
      })
      .catch(errMsg => this.$alert(errMsg.msg));
  }
};
</script>
<style lang="stylus" scoped>
.wrap {
  .search-row {
    .keyword {
      width: 500px;
    }

    .download {
      margin-left: 5px;
    }

    .row {
      margin-top: 5px;

      .date-desc, .org-desc {
        font-size: 18px;
        display: inline-block;
        margin-right: 5px;
        background-color: #ccc;
        border-radius: 5px;
        cursor: default;
      }

      .org-desc {
        margin-left: 15px;
      }
    }
  }
}
</style>